<template>
  <div class="wrapper">
    <img src="http://www.dell-lee.com/imgs/vue3/user.png"  class="wrapper__img">
    <div class="wrapper__input">
      <input type="text" class="wrapper__input__content" placeholder="请输入手机号" v-model="username">
    </div>
    <div class="wrapper__input">
      <input  class="wrapper__input__content" placeholder="请输入密码" type="password" v-model="password">
    </div>
    <div class="wrapper__input">
      <input   class="wrapper__input__content" placeholder="确认密码" type="password" v-model="ensurement">
    </div>
    <div class="wrapper__register-button" @click="handleRegister">注册</div>
    <div class="wrapper__register-link" @click="handleLoginClick">已有账号去登录</div>
  </div>
  <Toast v-if="show" :message="toastMessage"></Toast>
</template>

<script>
import {useRouter} from 'vue-router';
import {reactive, toRefs} from "vue";
import {post} from "@/utils/require.js";
import Toast,{useToastEffect} from "@/components/Toast.vue";

const useRegisterEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({
    username: '',
    password: '',
    ensurement:'',
  });
  const handleRegister = async () => {

    if (data.password != data.ensurement){
      showToast('ensurement有误')
      return
    }
    try {
      const result = await post('/register.php', {
        'username': data.username,
        'password': data.password
      })
      console.log(result)
      if (result?.errno == 0) {
        router.push({name: 'Login'})
      } else {
        console.log('注册失败')
        showToast('注册失败')
      }
    } catch (e) {
      console.log(1111)
      showToast('请求失败')
    }
  };
  const {username, password,ensurement} = toRefs(data)
  return {username, password, handleRegister,ensurement}
}

const useLoginEffect = ()=>{
  const router = useRouter();
  const handleLoginClick = () => {
    router.push({name:'Login'})
  };
  return {handleLoginClick}
}
 export default {
   name:'Register',
   components:{Toast},
   setup(){
    const {handleLoginClick} = toRefs(useLoginEffect())
     const {show, toastMessage, showToast} = useToastEffect();
     const {username, password, handleRegister,ensurement} = toRefs(useRegisterEffect(showToast))
     return {username, password,show, toastMessage, handleRegister,handleLoginClick,ensurement}
   }
 }
</script>

<style lang="scss" scoped>
@use '@/style/viriables' as *;
.wrapper{
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &__img{
    display: block;
    margin: 0 auto .4rem auto;
    width: .66rem;
    height: .66rem;
  }
  &__input{
    box-sizing: border-box;
    margin: 0 .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #F9F9F9;
    border: 1px solid #B7B7B7;
    border-radius: 6px;
    height: .48rem;
    &__content{
      line-height: .48rem;
      width: 100%;
      border: none;
      outline: none;
      background: none;
      font-size: .16rem;
      color: $content-noticecolor;
      &::placeholder{
        color: $content-noticecolor;
      }
    }
  }
  &__register-button{
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: $btn-bgColor;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
    border-radius: 4px;
    color: $bgColor;
    font-size: .16rem;
    text-align: center;
  }
  &__register-link{
    font-size: .14rem;
    text-align: center;
    color: $content-noticecolor;
  }
}
</style>